package patrol.mobile.com.mobilepatrol.activity.toll;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.dl7.recycler.listener.OnRequestDataListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.adapter.TrafficChargeAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.TrafficChargeBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil03;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;

/**
 * Created by K on 2018/9/14.
 * des: 交通量收费额
 */
public class TrafficChargeActivity extends CommonListActivity {
    private int page = 1;
    private List<TrafficChargeBean> trafficChargeBeans;
    private TextView tvStart;
//    private MySpinner mMySpinner01;
    private String currentTime = "";
    private String orgId;

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new TrafficChargeAdapter(this);
    }

    @Override
    public void initView() {
        super.initView();
        View itemView = LayoutInflater.from(this).inflate(R.layout.activity_my_evaluation_top, null);
        flContent.addView(itemView);
        tvStart = itemView.findViewById(R.id.tv_start);
//        mMySpinner01 = itemView.findViewById(R.id.mySpinner01);
        iv_right_title.setVisibility(View.VISIBLE);
        iv_right_title.setImageResource(R.mipmap.add_white);
        iv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(TrafficChargeUpDataActivity.class);
            }
        });
        tvStart.setText(TimeUtils.dateStringYM(System.currentTimeMillis()));
        currentTime = TimeUtils.dateStringYM(System.currentTimeMillis());
//        List<String> data = new ArrayList<>();
//        data.add("收费站");
//        mMySpinner01.setText("收费站");// TODO: 2019/3/6 0181增加收费站过滤
//        mMySpinner01.setmDatas(data, new MySpinner.CallBack() {
//            @Override
//            public void onSelect(int position) {
//                mMySpinner01.setText(data.get(position));
//            }
//        });
        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerUtil03.getInstance().show(TrafficChargeActivity.this, new CallBack<DateBean>() {
                    @Override
                    public void onBack(DateBean dateBean) {
                        currentTime = dateBean.getmDate();
                        tvStart.setText(dateBean.getmDate());
                        requestData(true);
                    }
                });
            }
        });
//        mMySpinner01.setmDatas(list, new MySpinner.CallBack() {
//            @Override
//            public void onSelect(int position) {
//                requestData(currentTime);
//                orgId = orgListBean.getList().get(position).getId();
//            }
//        });
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestData(true);
    }

    private void requestData(final boolean isRefresh) {
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        HttpSender sender = new HttpSender(this, baseUrl, Urls.GET_ORG_TRAFFIC_TOLL_DETAIL_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", UserManager.getOrgNo());
        map.put("period", currentTime);
        map.put("page", page + "");
        map.put("rows", ROWS + "");
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();

                JsonArray listArray = jo.getAsJsonArray("list");
                int rows = jo.get("rows").getAsInt();
                Log.i("ROWS", "onResponse: " + rows + "----" + Math.ceil(rows / ROWS));
                trafficChargeBeans = GsonUtil.jsonToList(listArray.toString(), TrafficChargeBean.class);
                setData(page, false, trafficChargeBeans, rows);
            }
        });
    }

    @Override
    public void setListener() {
        super.setListener();
        mAdapter.setRequestDataListener(new OnRequestDataListener() {
            @Override
            public void onLoadMore() {
                requestData(false);
            }
        });
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(TrafficChargeActivity.this, TrafficChargeDetailActivity.class);
                intent.putExtra("id", trafficChargeBeans.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected String initTitleName() {
        return "交通量收费额";
    }
}
